{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "SystemExit",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3426: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
      "  warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "from PyQt5.QtWidgets import *\n",
    "from PyQt5.QtGui import *\n",
    "from PyQt5.QtCore import *\n",
    "\n",
    "class QCalendarWidgetDemo(QWidget):\n",
    "    def __init__(self):\n",
    "\n",
    "        super(QCalendarWidgetDemo,self).__init__()\n",
    "\n",
    "        self.initUI()\n",
    "\n",
    "    def initUI(self):\n",
    "\n",
    "        self.calendar = QCalendarWidget(self) #这里没有加布局控件\n",
    "\n",
    "        # self.calendar.setGridVisible(True) #日历网格\n",
    "\n",
    "        self.calendar.setMinimumDate(QDate(1999,1,1,))\n",
    "\n",
    "        self.calendar.setMaximumDate(QDate(2099,1,1))\n",
    "\n",
    "        self.calendar.move(50,50)\n",
    "\n",
    "        self.setWindowTitle('日历控件')\n",
    "\n",
    "        self.calendar.clicked.connect(self.showDate)\n",
    "\n",
    "        self.resize(400,400)\n",
    "\n",
    "        date = self.calendar.selectedDate()\n",
    "\n",
    "        self.label = QLabel(self) #这里要加 继承关系\n",
    "\n",
    "        self.label.setText(date.toString('yyyy-MM-dd dddd'))\n",
    "\n",
    "        self.label.move(150,380)\n",
    "\n",
    "    def showDate(self):\n",
    "\n",
    "        self.label.setText((self.calendar.selectedDate().toString('yyyy-MM-dd dddd')))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    app = QApplication(sys.argv)\n",
    "\n",
    "    main = QCalendarWidgetDemo()\n",
    "\n",
    "    main.show()\n",
    "\n",
    "    sys.exit(app.exec_())\n",
    "\n",
    "\n",
    "\n",
    "#日历控件二 QDateTimeEdit\n",
    "\n",
    "import sys\n",
    "\n",
    "from PyQt5.QtWidgets import *\n",
    "\n",
    "from PyQt5.QtGui import *\n",
    "\n",
    "from PyQt5.QtCore import *\n",
    "\n",
    "class QDateTimeEditDemo(QWidget):\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        super(QDateTimeEditDemo,self).__init__()\n",
    "\n",
    "        self.initUI()\n",
    "\n",
    "    def initUI(self):\n",
    "\n",
    "        self.resize(100,100)\n",
    "\n",
    "        self.datetimeEdit = QDateTimeEdit()\n",
    "\n",
    "        self.datetimeEdit.setDisplayFormat('yyyy-MM-dd HH:mm:ss')\n",
    "\n",
    "        self.datetimeEdit.dateChanged.connect(self.dateChanged) #日期变化信号绑定槽 这里到底是怎么传进函数里的date参数的？\n",
    "\n",
    "        self.datetimeEdit.timeChanged.connect(self.timeChanged) #时间变化信号绑定槽\n",
    "\n",
    "        self.datetimeEdit.dateTimeChanged.connect(self.dateTimeChanged) #日期或时间变化绑定槽\n",
    "\n",
    "        self.datetimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime())\n",
    "\n",
    "        self.datetimeEdit2.setDisplayFormat('yyyy-MM-dd HH:mm:ss')\n",
    "\n",
    "        self.datetimeEdit3 = QDateTimeEdit(QDate.currentDate())\n",
    "\n",
    "        self.datetimeEdit3.setDisplayFormat('yyyy-MM-dd')\n",
    "\n",
    "        self.datetimeEdit4 = QDateTimeEdit(QTime.currentTime())\n",
    "\n",
    "        self.datetimeEdit4.setDisplayFormat('HH:mm:ss')\n",
    "\n",
    "        self.datetimeEdit5 = QDateTimeEdit()\n",
    "\n",
    "        self.datetimeEdit5.setCalendarPopup(True) #设置下拉框 弹出日历框 直接选择日期\n",
    "\n",
    "        self.datetimeEdit5.setDisplayFormat('yyyy-MM-dd HH:mm:ss')\n",
    "\n",
    "        self.button = QPushButton('获取日期和时间')\n",
    "\n",
    "        self.button.clicked.connect(self.buttonClicked)\n",
    "\n",
    "        layout = QVBoxLayout(self)\n",
    "\n",
    "        layout.addWidget(self.datetimeEdit)\n",
    "\n",
    "        layout.addWidget(self.datetimeEdit2)\n",
    "\n",
    "        layout.addWidget(self.datetimeEdit3)\n",
    "\n",
    "        layout.addWidget(self.datetimeEdit4)\n",
    "\n",
    "        layout.addWidget(self.datetimeEdit5)\n",
    "\n",
    "        layout.addWidget(self.button)\n",
    "\n",
    "    def dateChanged(self,date):\n",
    "\n",
    "        print(date) #PyQt5.QtCore.QDateTime(2001, 1, 1, 0, 0) #绑定信号和槽的时候，参数中self是类对象本身，\n",
    "\n",
    "        print(type(date)) #\n",
    "\n",
    "    def timeChanged(self,time):\n",
    "\n",
    "        print(time) #PyQt5.QtCore.QDate(2001, 1, 1)\n",
    "\n",
    "    def dateTimeChanged(self,dateTime):\n",
    "\n",
    "        print(dateTime)\n",
    "\n",
    "    def buttonClicked(self):\n",
    "\n",
    "        print(self.datetimeEdit.dateTime())\n",
    "\n",
    "        print(self.datetimeEdit.minimumDate())\n",
    "\n",
    "        print(self.datetimeEdit.maximumDateTime())\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    app = QApplication(sys.argv)\n",
    "\n",
    "    main = QDateTimeEditDemo()\n",
    "\n",
    "    main.show()\n",
    "\n",
    "    sys.exit(app.exec_())\n",
    "\n",
    "\n",
    "\n",
    "#菜单QMenuBar\n",
    "\n",
    "import sys\n",
    "\n",
    "from PyQt5.QtWidgets import *\n",
    "\n",
    "from PyQt5.QtGui import *\n",
    "\n",
    "from PyQt5.QtCore import *\n",
    "\n",
    "class MenuDemo(QMainWindow):\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        super(MenuDemo,self).__init__()\n",
    "\n",
    "        menu = QMenuBar(self)\n",
    "\n",
    "        file = menu.addMenu('文件')\n",
    "\n",
    "        file.addAction('新建')\n",
    "\n",
    "        save = QAction('保存',self)\n",
    "\n",
    "        save.setShortcut('Ctrl+S')\n",
    "\n",
    "        file.addAction(save)\n",
    "\n",
    "        save.triggered.connect(self.process)\n",
    "\n",
    "        edit = menu.addMenu('编辑')\n",
    "\n",
    "        edit.addAction('修改')\n",
    "\n",
    "        edit.addAction('删除')\n",
    "\n",
    "        edit.addAction('设置')\n",
    "\n",
    "        quit = QAction('退出',self)\n",
    "\n",
    "        edit.addAction(quit)\n",
    "\n",
    "        quit.triggered.connect(self.quit)\n",
    "\n",
    "    def process(self):\n",
    "\n",
    "        print(self.sender().text())\n",
    "\n",
    "    def quit(self):\n",
    "\n",
    "        self.close()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    app = QApplication(sys.argv)\n",
    "\n",
    "    main = MenuDemo()\n",
    "\n",
    "    main.show()\n",
    "\n",
    "    sys.exit(app.exec_())\n",
    "\n",
    "\n",
    "\n",
    "#创建和使用工具栏 工具栏三种显示状态：只显示图标，只显示文本，同时显示文本和图标，工具栏和菜单栏区别？菜单栏比较全，工具栏是提高效率响应快速，最好用图标\n",
    "\n",
    "import sys\n",
    "\n",
    "from PyQt5.QtWidgets import *\n",
    "\n",
    "from PyQt5.QtGui import *\n",
    "\n",
    "from PyQt5.QtCore import *\n",
    "\n",
    "class ToolDemo(QMainWindow):\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        super(ToolDemo,self).__init__()\n",
    "\n",
    "        self.initUI()\n",
    "\n",
    "    def initUI(self):\n",
    "\n",
    "        self.setWindowTitle('工具栏演示')\n",
    "\n",
    "        self.resize(300,200)\n",
    "\n",
    "        tb1 = self.addToolBar('File')\n",
    "\n",
    "        tb1.addAction(QAction(QIcon('./images/new.png'),'new',self))\n",
    "\n",
    "        tb1.addAction(QAction(QIcon('./images/open.png'),'open',self))\n",
    "\n",
    "        tb1.addAction(QAction(QIcon('./images/save.png'),'save',self))\n",
    "\n",
    "        tb2 = self.addToolBar('Edit')\n",
    "\n",
    "        tb2.addAction(QAction(QIcon('./images/setting.png'), 'setting', self))\n",
    "\n",
    "        tb2.addAction(QAction(QIcon('./images/back.png'), 'back', self))\n",
    "\n",
    "        tb2.addAction(QAction(QIcon('./images/forward.png'), 'forward', self))\n",
    "\n",
    "        tb2.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) #图标后面加文字 setToolButtonStyle\n",
    "\n",
    "        tb1.actionTriggered.connect(self.toolbtnpressed)#绑定信号和槽 绑定的信号是 actionTriggered\n",
    "\n",
    "        tb2.actionTriggered.connect(self.toolbtnpressed) # 同时绑定一起，在后台看下结果\n",
    "\n",
    "    def toolbtnpressed(self,btn):\n",
    "\n",
    "        print('按下了',btn.text())\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    app = QApplication(sys.argv)\n",
    "\n",
    "    main = ToolDemo()\n",
    "\n",
    "    main.show()\n",
    "\n",
    "    sys.exit(app.exec_())\n",
    "\n",
    "\n",
    "\n",
    "#状态栏 QStatusBar\n",
    "\n",
    "import sys\n",
    "\n",
    "from PyQt5.QtWidgets import *\n",
    "\n",
    "from PyQt5.QtGui import *\n",
    "\n",
    "from PyQt5.QtCore import *\n",
    "\n",
    "class StatusBarDemo(QMainWindow):\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        super(StatusBarDemo,self).__init__()\n",
    "\n",
    "        self.initUI()\n",
    "\n",
    "    def initUI(self):\n",
    "\n",
    "        self.setWindowTitle('菜单栏的演示')\n",
    "\n",
    "        self.resize(300,200)\n",
    "\n",
    "        self.statusBar = QStatusBar() #实例化状态栏\n",
    "\n",
    "        self.setStatusBar(self.statusBar) #放在主窗口\n",
    "\n",
    "        menu = self.menuBar() #实例化一个菜单栏\n",
    "\n",
    "        file = menu.addMenu('File') #菜单栏里添加一个菜单\n",
    "\n",
    "        file.addAction('show') #菜单栏添加一个动作\n",
    "\n",
    "        file.triggered.connect(self.processTriggered)\n",
    "\n",
    "    def processTriggered(self,q):\n",
    "\n",
    "        if q.text()=='show':\n",
    "\n",
    "            self.statusBar.showMessage(q.text()+'被点击了',5000)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    app = QApplication(sys.argv)\n",
    "\n",
    "    main = StatusBarDemo()\n",
    "\n",
    "    main.show()\n",
    "\n",
    "    sys.exit(app.exec_())\n",
    "\n",
    "\n",
    "\n",
    "#显示对话框【没成功】\n",
    "\n",
    "import sys\n",
    "\n",
    "from PyQt5.QtWidgets import *\n",
    "\n",
    "from PyQt5.QtGui import *\n",
    "\n",
    "from PyQt5.QtCore import *\n",
    "\n",
    "from PyQt5.QtPrintSupport import *\n",
    "\n",
    "class PrintDemo(QWidget): #这里用QWidget，layout才有效，用QMainWindow无效\n",
    "\n",
    "    def __init__(self):\n",
    "\n",
    "        super(PrintDemo,self).__init__()\n",
    "\n",
    "        self.resize(400,300)\n",
    "\n",
    "        self.setWindowTitle('使用打印机')\n",
    "\n",
    "        self.button = QPushButton(self)\n",
    "\n",
    "        self.button.setText('点击打印内容')\n",
    "\n",
    "        self.button.clicked.connect(self.print)\n",
    "\n",
    "        self.textEdit = QTextEdit(self)\n",
    "\n",
    "        # self.textEdit.geometry()\n",
    "\n",
    "        layout = QVBoxLayout(self)\n",
    "\n",
    "        layout.addWidget(self.button)\n",
    "\n",
    "        layout.addWidget(self.textEdit)\n",
    "\n",
    "    def print(self):\n",
    "\n",
    "        printer = QPrinter()\n",
    "\n",
    "        painter = QPainter()\n",
    "\n",
    "        painter.begin(printer)\n",
    "\n",
    "        screen = self.textEdit.grab()\n",
    "\n",
    "        painter.drawPixmap(screen)\n",
    "\n",
    "        painter.end()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "\n",
    "    app = QApplication(sys.argv)\n",
    "\n",
    "    main = PrintDemo()\n",
    "\n",
    "    main.show()\n",
    "\n",
    "    sys.exit(app.exec_())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
